home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
FromTheMag
/
JW FLV MEDIA PLAYER 4.2
/
mediaplayer.exe
/
player.swf
/
scripts
/
com
/
jeroenwijering
/
player
/
Controller.as
next >
Wrap
Text File
|
2008-11-04
|
12KB
|
386 lines
package com.jeroenwijering.player
{
import com.jeroenwijering.events.ControllerEvent;
import com.jeroenwijering.events.ModelEvent;
import com.jeroenwijering.events.ModelStates;
import com.jeroenwijering.events.ViewEvent;
import com.jeroenwijering.utils.Playlister;
import com.jeroenwijering.utils.Randomizer;
import flash.display.MovieClip;
import flash.events.ErrorEvent;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.geom.Rectangle;
import flash.net.URLRequest;
import flash.net.navigateToURL;
import flash.system.Capabilities;
public class Controller extends EventDispatcher
{
private var config:Object;
private var randomizer:Randomizer;
private var view:View;
private var model:Model;
private var skin:MovieClip;
private var playlister:Playlister;
public function Controller(param1:Object, param2:MovieClip)
{
super();
config = param1;
skin = param2;
playlister = new Playlister();
playlister.addEventListener(Event.COMPLETE,playlistHandler);
playlister.addEventListener(ErrorEvent.ERROR,errorHandler);
config["controlbarsize"] = skin.controlbar.height;
redrawHandler();
}
private function metaHandler(param1:ModelEvent) : void
{
var _loc2_:Number = NaN;
if(param1.data.duration)
{
_loc2_ = Math.round(param1.data.duration * 10) / 10;
playlister.update(config["item"],"duration",_loc2_);
}
}
private function loadHandler(param1:ViewEvent) : void
{
var evt:ViewEvent = param1;
stopHandler();
try
{
playlister.load(evt.data.object);
}
catch(err:Error)
{
dispatchEvent(new ControllerEvent(ControllerEvent.ERROR,{"message":err.message}));
}
}
private function volumeHandler(param1:ViewEvent) : void
{
var _loc2_:Number = NaN;
_loc2_ = param1.data.percentage;
if(_loc2_ < 1)
{
muteHandler(new ViewEvent(ViewEvent.MUTE,{"state":true}));
}
else if(!isNaN(_loc2_) && _loc2_ < 101)
{
if(config["mute"] == true)
{
muteHandler(new ViewEvent(ViewEvent.MUTE,{"state":false}));
}
config["volume"] = _loc2_;
dispatchEvent(new ControllerEvent(ControllerEvent.VOLUME,{"percentage":_loc2_}));
}
}
private function redrawHandler(param1:ViewEvent = null) : void
{
var dat:Object = null;
var dps:String = null;
var evt:ViewEvent = param1;
dat = new Object();
try
{
dps = skin.stage["displayState"];
}
catch(err:Error)
{
}
if(dps == "fullScreen")
{
dat.fullscreen = true;
dat.width = skin.stage.stageWidth;
dat.height = skin.stage.stageHeight;
}
else if(config["resizing"])
{
dat.fullscreen = false;
dat.width = skin.stage.stageWidth;
dat.height = skin.stage.stageHeight;
if(config["controlbar"] == "bottom")
{
dat.height -= config["controlbarsize"];
}
if(config["playlist"] == "right")
{
dat.width -= config["playlistsize"];
}
else if(config["playlist"] == "bottom")
{
dat.height -= config["playlistsize"];
}
}
else
{
dat.fullscreen = false;
dat.width = config["width"];
dat.height = config["height"];
}
config["width"] = dat.width;
config["height"] = dat.height;
dispatchEvent(new ControllerEvent(ControllerEvent.RESIZE,dat));
}
private function fullscreenHandler(param1:ViewEvent) : void
{
if(skin.stage["displayState"] == "fullScreen")
{
skin.stage["displayState"] = "normal";
}
else
{
fullscreenrect();
skin.stage["displayState"] = "fullScreen";
}
}
private function nextHandler(param1:ViewEvent) : void
{
if(playlist && config["shuffle"] == true)
{
playItem(randomizer.pick());
}
else if(playlist && config["item"] == playlist.length - 1)
{
playItem(0);
}
else if(playlist)
{
playItem(config["item"] + 1);
}
}
private function qualityHandler(param1:ViewEvent = null) : void
{
if(param1.data.state != undefined)
{
if(param1.data.state == config["quality"])
{
return;
}
config["quality"] = param1.data.state;
}
else
{
config["quality"] = !config["quality"];
}
dispatchEvent(new ControllerEvent(ControllerEvent.QUALITY,{"state":config["quality"]}));
}
private function playlistHandler(param1:Event) : void
{
if(config["shuffle"] == true)
{
randomizer = new Randomizer(playlist.length);
config["item"] = randomizer.pick();
}
else if(config["item"] > playlist.length)
{
config["item"] = playlist.length - 1;
}
dispatchEvent(new ControllerEvent(ControllerEvent.PLAYLIST,{"playlist":playlist}));
if(config["autostart"] == true)
{
playItem();
}
}
private function fullscreenrect() : void
{
try
{
skin.stage["fullScreenSourceRect"] = new Rectangle(skin.x,skin.y,Capabilities.screenResolutionX / 2,Capabilities.screenResolutionY / 2);
}
catch(err:Error)
{
}
}
public function start(param1:Model, param2:View) : void
{
model = param1;
model.addEventListener(ModelEvent.META,metaHandler);
model.addEventListener(ModelEvent.TIME,metaHandler);
model.addEventListener(ModelEvent.STATE,stateHandler);
view = param2;
view.addEventListener(ViewEvent.FULLSCREEN,fullscreenHandler);
view.addEventListener(ViewEvent.ITEM,itemHandler);
view.addEventListener(ViewEvent.LINK,linkHandler);
view.addEventListener(ViewEvent.LOAD,loadHandler);
view.addEventListener(ViewEvent.MUTE,muteHandler);
view.addEventListener(ViewEvent.NEXT,nextHandler);
view.addEventListener(ViewEvent.PLAY,playHandler);
view.addEventListener(ViewEvent.PREV,prevHandler);
view.addEventListener(ViewEvent.QUALITY,qualityHandler);
view.addEventListener(ViewEvent.REDRAW,redrawHandler);
view.addEventListener(ViewEvent.SEEK,seekHandler);
view.addEventListener(ViewEvent.STOP,stopHandler);
view.addEventListener(ViewEvent.VOLUME,volumeHandler);
}
private function prevHandler(param1:ViewEvent) : void
{
if(config["item"] == 0)
{
playItem(playlist.length - 1);
}
else
{
playItem(config["item"] - 1);
}
}
private function playItem(param1:Number = undefined) : void
{
if(param1 > -1)
{
if(playlist[param1]["file"] == playlist[config["item"]]["file"])
{
playlist[param1]["duration"] = playlist[config["item"]]["duration"];
}
config["item"] = param1;
}
dispatchEvent(new ControllerEvent(ControllerEvent.ITEM,{"index":config["item"]}));
}
private function stopHandler(param1:ViewEvent = undefined) : void
{
dispatchEvent(new ControllerEvent(ControllerEvent.STOP));
}
public function get playlist() : Array
{
return playlister.playlist;
}
private function linkHandler(param1:ViewEvent) : void
{
var _loc2_:Number = NaN;
var _loc3_:String = null;
_loc2_ = config["item"];
if(param1.data.index)
{
_loc2_ = param1.data.index;
}
_loc3_ = playlist[_loc2_]["link"];
if(_loc3_ != null)
{
navigateToURL(new URLRequest(_loc3_),config["linktarget"]);
}
}
private function errorHandler(param1:ErrorEvent) : void
{
dispatchEvent(new ControllerEvent(ControllerEvent.ERROR,{"message":param1.text}));
}
private function seekHandler(param1:ViewEvent) : void
{
var _loc2_:Number = NaN;
if(config["state"] != ModelStates.IDLE && playlist[config["item"]]["duration"] > 0)
{
_loc2_ = param1.data.position;
if(_loc2_ < 2)
{
_loc2_ = 0;
}
else if(_loc2_ > playlist[config["item"]]["duration"] - 2)
{
_loc2_ = playlist[config["item"]]["duration"] - 2;
}
dispatchEvent(new ControllerEvent(ControllerEvent.SEEK,{"position":_loc2_}));
}
}
private function playHandler(param1:ViewEvent) : void
{
if(playlist)
{
if(param1.data.state != false && config["state"] == ModelStates.PAUSED)
{
dispatchEvent(new ControllerEvent(ControllerEvent.PLAY,{"state":true}));
}
else if(param1.data.state != false && config["state"] == ModelStates.COMPLETED)
{
dispatchEvent(new ControllerEvent(ControllerEvent.SEEK,{"position":playlist[config["item"]]["start"]}));
}
else if(param1.data.state != false && config["state"] == ModelStates.IDLE)
{
playItem();
}
else if(param1.data.state != true && (config["state"] == ModelStates.PLAYING || config["state"] == ModelStates.BUFFERING))
{
dispatchEvent(new ControllerEvent(ControllerEvent.PLAY,{"state":false}));
}
}
}
private function itemHandler(param1:ViewEvent) : void
{
var _loc2_:Number = NaN;
_loc2_ = param1.data.index;
if(_loc2_ < 0)
{
playItem(0);
}
else if(_loc2_ > playlist.length - 1)
{
playItem(playlist.length - 1);
}
else if(!isNaN(_loc2_))
{
playItem(_loc2_);
}
}
private function muteHandler(param1:ViewEvent) : void
{
if(param1.data.state)
{
if(param1.data.state == config["mute"])
{
return;
}
config["mute"] = param1.data.state;
}
else
{
config["mute"] = !config["mute"];
}
dispatchEvent(new ControllerEvent(ControllerEvent.MUTE,{"state":config["mute"]}));
}
private function stateHandler(param1:ModelEvent) : void
{
if(param1.data.newstate == ModelStates.COMPLETED && (config["repeat"] == "always" || config["repeat"] == "list" && config["shuffle"] == true && randomizer.length > 0 || config["repeat"] == "list" && config["shuffle"] == false && config["item"] < playlist.length - 1))
{
if(config["shuffle"] == true)
{
playItem(randomizer.pick());
}
else if(config["item"] == playlist.length - 1)
{
playItem(0);
}
else
{
playItem(config["item"] + 1);
}
}
}
}
}